package cn.iocoder.yudao.module.erp.dal.mysql.dp;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.erp.controller.admin.dp.dpdx.vo.DpDxPageReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dp.DpDxDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.dp.DpYwDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Arrays;
import java.util.List;

/**
 * ERP东线口岸仓库制单 Mapper
 * @author 天使管理员
 */
@Mapper
public interface DpDxMapper extends BaseMapperX<DpDxDO> {

    default PageResult<DpDxDO> selectPage(DpDxPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DpDxDO>()
                .likeIfPresent(DpDxDO::getId, reqVO.getId())
                .likeIfPresent(DpDxDO::getNo, reqVO.getNo())
                .eqIfPresent(DpDxDO::getStoreType, reqVO.getStoreType())
                .likeIfPresent(DpDxDO::getInName, reqVO.getInName())
                .eqIfPresent(DpDxDO::getDispatchNumber, reqVO.getDispatchNumber())
                .eqIfPresent(DpDxDO::getInCount, reqVO.getInCount())
                .eqIfPresent(DpDxDO::getInVolume, reqVO.getInVolume())
                .eqIfPresent(DpDxDO::getWeight, reqVO.getWeight())
                .eqIfPresent(DpDxDO::getRemCount, reqVO.getRemCount())
                .eqIfPresent(DpDxDO::getRemVolume, reqVO.getRemVolume())
                .eqIfPresent(DpDxDO::getRemWeight, reqVO.getRemWeight())
                .eqIfPresent(DpDxDO::getTotalNote, reqVO.getTotalNote())
                .eqIfPresent(DpDxDO::getDensity, reqVO.getDensity())
                .eqIfPresent(DpDxDO::getDeliveryPhone, reqVO.getDeliveryPhone())
                .likeIfPresent(DpDxDO::getDeliveryName, reqVO.getDeliveryName())
                .eqIfPresent(DpDxDO::getEarpayAmount, reqVO.getEarpayAmount())
                .eqIfPresent(DpDxDO::getInPic, reqVO.getInPic())
                .eqIfPresent(DpDxDO::getLine, reqVO.getLine())
                .eqIfPresent(DpDxDO::getFile, reqVO.getFile())
                .eqIfPresent(DpDxDO::getNote, reqVO.getNote())
                .likeIfPresent(DpDxDO::getShipper, reqVO.getShipper())
                .eqIfPresent(DpDxDO::getDestination, reqVO.getDestination())
                .eqIfPresent(DpDxDO::getConsignee, reqVO.getConsignee())
                .eqIfPresent(DpDxDO::getAdvancePayPack, reqVO.getAdvancePayPack())
                .eqIfPresent(DpDxDO::getAdvancePayPackUp, reqVO.getAdvancePayPackUp())
                .eqIfPresent(DpDxDO::getAdvancePayChinaFreight, reqVO.getAdvancePayChinaFreight())
                .eqIfPresent(DpDxDO::getAdvancePayForGoods, reqVO.getAdvancePayForGoods())
                .eqIfPresent(DpDxDO::getCollecting, reqVO.getCollecting())
                .eqIfPresent(DpDxDO::getCustomerRebate, reqVO.getCustomerRebate())
                .eqIfPresent(DpDxDO::getBusinessRebate, reqVO.getBusinessRebate())
                .eqIfPresent(DpDxDO::getStatus, reqVO.getStatus())
                .eqIfPresent(DpDxDO::getPayType, reqVO.getPayType())
                .eqIfPresent(DpDxDO::getSalesman, reqVO.getSalesman())
                .eqIfPresent(DpDxDO::getUnitPrice, reqVO.getUnitPrice())
                .eqIfPresent(DpDxDO::getCalmethod, reqVO.getCalmethod())
                .eqIfPresent(DpDxDO::getTotalAmount, reqVO.getTotalAmount())
                .eqIfPresent(DpDxDO::getIsSplit, reqVO.getIsSplit())
                .eqIfPresent(DpDxDO::getInventoryStatus, reqVO.getInventoryStatus())
                .betweenIfPresent(DpDxDO::getCreateTime, reqVO.getCreateTime())
                .betweenIfPresent(DpDxDO::getChooseDate, reqVO.getChooseDateStr())
                .orderByDesc(DpDxDO::getId));
    }

    default PageResult<DpDxDO> getDpDxPage(DpDxPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DpDxDO>()
                .likeIfPresent(DpDxDO::getId, reqVO.getId())
                .inIfPresent(DpDxDO::getId,reqVO.getIds())
                .likeIfPresent(DpDxDO::getNo, reqVO.getNo())
                .betweenIfPresent(DpDxDO::getCreateTime, reqVO.getCreateTime())
                .betweenIfPresent(DpDxDO::getChooseDate, reqVO.getChooseDateStr())
                .eqIfPresent(DpDxDO::getCreator, reqVO.getCreator())
                .eqIfPresent(DpDxDO::getStatus, reqVO.getStatus())
                .eqIfPresent(DpDxDO::getLine, reqVO.getLine())
                .likeIfPresent(DpDxDO::getShipper, reqVO.getShipper())
                .inIfPresent(DpDxDO::getStatus,0,1)
                // 添加条件，查找 remCount 大于 0 的数据
                .gt(DpDxDO::getRemCount, 0)
                .orderByDesc(DpDxDO::getId));
    }

    default PageResult<DpDxDO> selectRemCountNoZeroPage(DpDxPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DpDxDO>()
                .likeIfPresent(DpDxDO::getId, reqVO.getId())
                .likeIfPresent(DpDxDO::getNo, reqVO.getNo())
                .betweenIfPresent(DpDxDO::getChooseDate, reqVO.getChooseDateStr())
                .eqIfPresent(DpDxDO::getCreator, reqVO.getCreator())
                .eqIfPresent(DpDxDO::getLine, reqVO.getLine())
                .eqIfPresent(DpDxDO::getStatus,1)
                //找出入库状态为0或2的
//                .in(DpDxDO::getInventoryStatus, Arrays.asList(0, 2))
                // 添加条件，查找 remCount 大于 0 的数据
                .gt(DpDxDO::getRemCount, 0)
                .orderByDesc(DpDxDO::getId));
    }

    @Select("<script>" +
            "SELECT temp.id FROM " +
            "(<foreach collection='ids' item='id' separator=' UNION ALL '>" +
            "SELECT #{id} AS id " +
            "</foreach>) temp " +
            "LEFT JOIN erp_dp_dx ON temp.id = erp_dp_dx.id " +
            "WHERE erp_dp_dx.id IS NULL" +
            "</script>")
    List<String> getNotExistIds(@Param("ids") List<String> ids);
}